package com.qiantu.phone.widget;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceState;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.uiutil.DeviceType;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


/**
 * Created by luzx on 2021/12/16.
 * 首页调光大view
 */


public class HomeCurtainBigView extends HomeDeviceBaseView {

//    private TextView tvBigOffline;
    private TextView tvValueBig;
    private CustomSeekCurtain seekBar;
    private ImageView imgDeviceFlag;
    private DeviceState mDeviceState;
    private DeviceState.StatusDTO mStatus;

    public HomeCurtainBigView(Context context) {
        super(context, null);
    }

    public HomeCurtainBigView(Context context, int type) {
        super(context, type);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.home_big_curtain_item;
    }

    protected void initView() {
        isBigView(true);
        super.initView();
        //bigView离线状态view重新赋值
//        tvBigOffline = findViewById(R.id.tv_offline_big);
        tvValueBig = findViewById(R.id.tv_value_big);
        seekBar = findViewById(R.id.seek_bar);
        imgDeviceFlag = findViewById(R.id.tv_out_device_flag);
        seekBar.setOnSeekBarChangeListener(new CustomSeekCurtain.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(int progress) {
                int strokePercentage = 100 - progress;
                setProgress(strokePercentage);
            }

            @Override
            public void onStartTrackingTouch() {

            }

            @Override
            public void onStopTrackingTouch(int progress) {
                int strokePercentage = 100 - progress;
                setProgress(strokePercentage);
//                DeviceState deviceState = mDevice.getStateData();
//                Integer oldStrokePercentage = deviceState.getStatus().getStrokepercentage();
//                float oldPercentage = Float.valueOf(oldStrokePercentage);
////                if(strokePercentage > 0){
////                    btnSwitch.setChecked(true);
////                }else{
////                    btnSwitch.setChecked(false);
////                }
//                if (oldPercentage != strokePercentage) {
//                    strokePercentageOperation(strokePercentage);
//                }
                strokePercentageOperation(strokePercentage);
            }
        });
    }

    private void setProgress(int progress){
        tvValueBig.setText(progress + "%");
    }

    @Override
    protected void onSwitchOperationSucceed(boolean isOpen) {
    }

    protected void updateView() {
        super.updateView();
        if(mDevice.getDeviceType().equals(DeviceType.WINDOW_CURTAINS.stringType) || mDevice.getDeviceType().equals("oc_windowcurtains")){
            seekBar.setCurtainLeftDrawable(getResources().getDrawable(R.drawable.home_curtain_left_drawable, getContext().getTheme()));
            seekBar.setCurtainRightDrawable(getResources().getDrawable(R.drawable.home_curtain_right_drawable, getContext().getTheme()));
        }else if(mDevice.getDeviceType().equals(DeviceType.ROLLER_SHUTTER.stringType)){
            seekBar.setCurtainLeftDrawable(getResources().getDrawable(R.drawable.home_curtain_left_drawable, getContext().getTheme()));
            seekBar.setCurtainRightDrawable(null);
        }
        mDeviceState = mDevice.getStateData();
        mStatus = mDeviceState.getStatus();
        Integer strokePercentage = mStatus.getStrokepercentage();
        Float progress = Float.valueOf(strokePercentage);
//        if (mDevice.isIsOnline()) {
//            tvBigOffline.setVisibility(GONE);
//            tvValueBig.setVisibility(VISIBLE);
            seekBar.setProgress(100 - progress);
            setProgress(progress.intValue());
            seekBar.setOpen(true);
//            if(progress > 0){
//                btnSwitch.setChecked(true);
//            }else{
//                btnSwitch.setChecked(false);
//            }
//        } else {
//            tvBigOffline.setVisibility(VISIBLE);
//            tvValueBig.setVisibility(GONE);
//        }
//        seekBar.isOnline(mDevice.isIsOnline());

        imgDeviceFlag.setVisibility(mDevice.getIsFavourite() ? VISIBLE : INVISIBLE);
    }

    /**
     * 调节开合度
     * @param strokePercentage
     */
    private void strokePercentageOperation(int strokePercentage){
        Map<String, Object> option = new HashMap<>();
        option.put(LLDeviceUtil.STROKE_PERCENTAGE, strokePercentage);
        Map<String, Object> status = new HashMap<>();
        status.put(LLDeviceUtil.STATUS, option);
        LLHttpManager.operationDevice((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                mDevice.setStrokeStatePercentage(strokePercentage);
                mDevice.setStrokeState(strokePercentage > 0 ? LLDeviceUtil.CURTAIN_OPEN : LLDeviceUtil.CURTAIN_CLOSE);
                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
            }

            @Override
            public void onFail(Exception e) {
                //取设备原来进度
                Integer strokePercentage = mStatus.getStrokepercentage();
                Float progress = Float.valueOf(strokePercentage);
                seekBar.setProgress(100 - progress);
                setProgress(progress.intValue());
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

//    /**
//     * 开关调节开合度
//     */
//    protected void switchOperation(){
//        Map<String, Object> option = new HashMap<>();
//        Integer strokeState = btnSwitch.isChecked() ? LLDeviceUtil.CURTAIN_CLOSE : LLDeviceUtil.CURTAIN_OPEN;
//        option.put(LLDeviceUtil.STROKE_STATE, strokeState);
//        Map<String, Object> status = new HashMap<>();
//        status.put(LLDeviceUtil.STATUS, option);
//        LLHttpManager.operationDevice((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<Void>>(null) {
//            @Override
//            public void onStart(Call call) {
//                ((AppActivity) getContext()).showDialog("执行中...");
//            }
//
//            @Override
//            public void onSucceed(HttpData<Void> result) {
////                btnSwitch.setChecked(!btnSwitch.isChecked());
//                mDevice.setStrokeState(strokeState);
//                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
//            }
//
//            @Override
//            public void onFail(Exception e) {
//                ToastUtils.show(R.string.exec_failure);
//            }
//
//            @Override
//            public void onEnd(Call call) {
//                ((AppActivity) getContext()).hideDialog();
//            }
//        });
//    }
}
